﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http.Filters;
using JWT;
using Newtonsoft.Json;
using Zhj.Community.Infrastructure.Common;
using Zhj.Community.Infrastructure.WebApi;
using Zhj.Community.WebApi.Models;

namespace Zhj.Community.WebApi.Filter
{
    public class ApiTokenFilter: ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            //iss：Issuer，发行者
            //sub：Subject，主题
            //aud：Audience，观众
            //exp：Expiration time，过期时间
            //nbf：Not before
            //iat：Issued at，发行时间
            //jti：JWT ID
            try
            {
                var request = actionContext.Request;
                string userstr;
                var cookieValue = request.Headers.GetCookies().FirstOrDefault();
                var cookie = cookieValue["token"];
                if (cookie == null || string.IsNullOrEmpty(cookie.Value))
                {
                    IEnumerable<string> values;
                    if (request.Headers.TryGetValues("Authorizatio", out values))
                    {
                        var str = values.FirstOrDefault();
                        userstr = str;
                    }
                    else
                    {
                        ResultMsg resultMsg = new ResultMsg
                        {
                            StatusCode = (int)StatusCodeEnum.TokenInvalid,
                            Info = StatusCodeEnum.TokenInvalid.GetEnumText(),
                            Data = string.Empty
                        };
                        actionContext.Response = HttpResponseExtension.ToJson(JsonConvert.SerializeObject(resultMsg));
                        base.OnActionExecuting(actionContext);
                        return;
                    }
                }
                else
                {
                    userstr = cookie.Value;
                }
                string userinfo = JsonWebToken.Decode(userstr, WebSettingsConfig.TokenKey);
                Token info = SerializeHelper.SerializeToObject<Token>(userinfo);
                //处理用户信息
            }
            catch (SignatureVerificationException ex)
            {
                ResultMsg resultMsg = new ResultMsg
                {
                    StatusCode = (int)StatusCodeEnum.TokenInvalid,
                    Info = StatusCodeEnum.TokenInvalid.GetEnumText(),
                    Data = ex
                };
                actionContext.Response = HttpResponseExtension.ToJson(JsonConvert.SerializeObject(resultMsg));
                base.OnActionExecuting(actionContext);
            }
            catch (Exception ex)
            {
                ResultMsg resultMsg = new ResultMsg
                {
                    StatusCode = (int)StatusCodeEnum.TokenInvalid,
                    Info = StatusCodeEnum.TokenInvalid.GetEnumText(),
                    Data = ex
                };
                actionContext.Response = HttpResponseExtension.ToJson(JsonConvert.SerializeObject(resultMsg));
                base.OnActionExecuting(actionContext);
            }
            
        }
    }
}